package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.VehicleApplication;
import com.amsp.oa.oademo.service.VehicleService;
import com.amsp.oa.oademo.common.Result;
import com.amsp.oa.oademo.model.request.VehicleDispatchRequest;
import com.amsp.oa.oademo.model.request.VehicleRecordRequest;
import com.amsp.oa.oademo.model.request.VehicleReturnRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/vehicle")
public class VehicleController {

    @Autowired
    private VehicleService vehicleService;

    @PostMapping("/application")
    public Result<String> createApplication(@RequestBody VehicleApplication application) {
        try {
            String applyNo = vehicleService.createApplication(application);
            return Result.ok(applyNo);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/application/{id}/approve")
    public Result<Void> approveApplication(
            @PathVariable Long id,
            @RequestParam String result,
            @RequestParam String remark) {
        try {
            vehicleService.approveApplication(id, result, remark);
            return Result.ok();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/applications")
    public Result<List<VehicleApplication>> getApplicationList(@RequestParam String status) {
        try {
            List<VehicleApplication> list = vehicleService.getApplicationList(status);
            return Result.ok(list);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/application/{id}")
    public Result<VehicleApplication> getApplicationById(@PathVariable Long id) {
        try {
            VehicleApplication application = vehicleService.getApplicationById(id);
            return Result.ok(application);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/pending-approvals")
    public Result<List<VehicleApplication>> getPendingApprovals() {
        try {
            List<VehicleApplication> list = vehicleService.getPendingApprovals();
            return Result.ok(list);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/approved-applications")
    public Result<List<VehicleApplication>> getApprovedApplications() {
        try {
            List<VehicleApplication> list = vehicleService.getApprovedApplications();
            return Result.ok(list);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    // 派遣相关接口
    @PostMapping("/dispatch/{id}")
    public Result<Void> dispatchVehicle(@PathVariable Long id, @RequestBody VehicleDispatchRequest request) {
        try {
            vehicleService.dispatchVehicle(id, request);
            return Result.ok();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @GetMapping("/pending-dispatch")
    public Result<List<VehicleApplication>> getPendingDispatchList() {
        try {
            return Result.ok(vehicleService.getPendingDispatchList());
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    // 用车记录相关接口
    @PostMapping("/record/{id}")
    public Result<Void> recordVehicleUsage(@PathVariable Long id, @RequestBody VehicleRecordRequest request) {
        try {
            vehicleService.recordVehicleUsage(id, request);
            return Result.ok();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @GetMapping("/in-use")
    public Result<List<VehicleApplication>> getInUseList() {
        try {
            return Result.ok(vehicleService.getInUseList());
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    // 归还确认相关接口
    @PostMapping("/return/{id}/confirm")
    public Result<Void> confirmReturn(@PathVariable Long id, @RequestBody VehicleReturnRequest request) {
        try {
            vehicleService.confirmReturn(id, request);
            return Result.ok();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @GetMapping("/pending-return")
    public Result<List<VehicleApplication>> getPendingReturnList() {
        try {
            return Result.ok(vehicleService.getPendingReturnList());
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
} 